
// 首页轮播
new Vue({

    data:{
        bannerSwiper:null
    },


    methods:{
        next:function(){
            this.bannerSwiper.swipeNext();
        },
        prev:function(){
            this.bannerSwiper.swipePrev();
        }
    },

    mounted: function () {
        this.bannerSwiper = this.$createSwiper(this.$el, {
            pagination: '.swiper-pagination',
            paginationClickable: false,
            createPagination :false, // 不使用分页
           // simulateTouch: false,
           //  nextButton: '.swiper-button-next',
           //  prevButton: '.swiper-button-prev',
            speed: 500,
            loop: true,
            autoplay: 5000, //可选选项，自动滑动
        })
    }

}).$mount('#index_banner_wrapper');



//产品服务轮播
new Vue({

    data:{
        swiper:null
    },


    methods:{
        next:function(){

            this.swiper.swipeNext();
        },
        prev:function(){
            this.swiper.swipePrev();
        }
    },

    mounted: function () {
        this.swiper = this.$createSwiper(this.$refs['product-sevice-swiper'], {
            slidesPerView: 5,
            paginationClickable: true,
            // nextButton: '#product_sevice .swiper-button-next',
            // prevButton: '#product_sevice .swiper-button-prev',
            loop: false,
            simulateTouch: false,
            spaceBetween: 10,
            // autoplay: 5000, //可选选项，自动滑动
        })
    }

}).$mount('#product_sevice');

//新闻模块
new Vue({

    
    data:{

        loading : true, //请求状态

        sub_types: [], //新闻类型

        articles : {}, //新闻相关信息

        article_list : [], //新闻列表

        newsSwiper:null,

        article_type_id : '',

        news_detail : {},  //新闻详情

    },

    mounted: function() {

        this.fetchNewsType()
            .then(this.hadArticleTypeId)

    },

    methods:{

        next:function(){

            this.newsSwiper.swipeNext();

        },

        prev:function(){

            this.newsSwiper.swipePrev();

        },

        
        /**
         * 获取新闻列表
         * @return {undefined}
         */
        fetchNewsList : function fetchNewsList(){

            this.loading = true;

            return this.$http.get('/api/home/company-news?page=1&per_page=8&article_type_id=' + this.article_type_id )

                    .then(function(res){

                        if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                        this.articles = res.data.articles;

                        this.article_list = res.data.articles.data;

                    })

                    .catch(function(error) {

                        this.$Message.error(error.message);

                    })

                    .then(function(){

                        this.loading = false;

                    })

                    .then(function() {
                        
                        this.newsSwiper = this.$createSwiper(this.$refs['news-swiper'], {
                            // nextButton: '.news-banner-wrapper .swiper-button-next',
                            // prevButton: '.news-banner-wrapper .swiper-button-prev',
                            pagination: '.news-banner-wrapper .swiper-pagination',
                            simulateTouch: false,
                            slidesPerView: 1,
                            paginationClickable: true,
                            // autoplay: 4000, //可选选项，自动滑动
                            // observer:true,
                            // loop: true
                        });         //配置轮播插件
                    });
      
        },

        /**
         * 获取新闻类型
         * @return {undefined} 
         */
        fetchNewsType: function fetchNewsType() {
            
            this.loading = true;

            return this.$http.get('/api/home/company-news/create')

                    .then(function(res){

                        if( parseInt(res.data.code) !== 0 )throw new Error(res.data.error);

                        this.sub_types = res.data.sub_types;

                    })

                    .catch(function(error){

                        this.$Message.error(error.message);

                    })

                    .then(function(){

                        this.loading = false;

                    })

        },

        /**
         * 判断文章类别是否有值
         * @return {undefined}
         */
        hadArticleTypeId : function hadArticleTypeId(){

            if( !this.article_type_id ) {

                this.article_type_id = this.sub_types[0].id;

                this.changeTag(this.article_type_id);

                return;
            }

        },
    
        /**
         * 
         * 新闻类型选择
         * @param {String} id   新闻类型的id
         */
        changeTag: function changeTag(id) {

            if (this.loading) return;

            this.article_type_id = id;

            return this.fetchNewsList();

        },

        /**
         * 显示新闻详情
         * @param  {Object} record {新闻对象}
         * @return {undefined}
         */
        watchNewsDetail: function watchNewsDetail(record){

            this.news_detail = record;

        },

    },

    updated: function(){
        // 模板跟新后，重新初始化组件
        this.newsSwiper && this.newsSwiper.reInit();
    },
       
}).$mount("#news");




